package cn.edu.zhku.citrusmonitor.person.user.service;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import cn.edu.zhku.citrusmonitor.domain.TbUser;
import cn.edu.zhku.citrusmonitor.person.user.dao.UserDao;
import cn.edu.zhku.citrusmonitor.person.user.dao.UserDaoImpl;

public class UserServiceImpl implements UserService{
	UserDao ud=new UserDaoImpl();
	/**
	 * 添加用户
	 */
	@Override
	public String addUser(TbUser user) {
		try {
			ud.add(user);
			return "success";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "failure";
		}
	}
	/**
	 * 通过手机查找
	 */
	@Override
	public TbUser searchByPhone(String phone) {
		// TODO Auto-generated method stub
		try {
			return ud.getUserByPhone(phone);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	/**
	 * 登陆
	 */
	@Override
	public TbUser login(String phone, String password) {
		// TODO Auto-generated method stub
		try {
			return ud.getUserByPhoneAndPwd(phone,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 获得总记录数
	 */
	@Override
	public long getTotalCounts() {
		
		try {
			return ud.getTotal();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	/**
	 * 获得对象集合
	 */
	@Override
	public List<TbUser> getBeanList(int currentPage, long pageSize) {
		
		try {
			return ud.getList(currentPage,pageSize);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	
	/**
	 * 获取用户id
	 */
	/*@Override
	public String getUserId(TbUser user) {
		// TODO Auto-generated method stub
		ud.getUserIdByUser(user);
		return null;
	}*/
	/**
	 * 修改用户信息
	 */
	@Override
	public int modifyUser(TbUser user) {
		// TODO Auto-generated method stub
		try {
			return ud.modify(user);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	@Override
	public int deleteUser(String id) {
		// TODO Auto-generated method stub
		try {
			return ud.deleteUserById(id);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	@Override
	public TbUser getUser(String id) {
		try {
			return ud.getUserById(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
	}
	@Override
	public List<TbUser> getUserByPhone(String phone) {
		// TODO Auto-generated method stub
		try {
			return ud.getUserByLikePhone(phone);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	@Override
	public long removeAll(String[] ids) {
		// TODO Auto-generated method stub
		try {
			int[] nums=ud.removeAllById(ids);
			
			long counts=0;
			for(int i:nums) {
				counts+=i;
			}
			return counts;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
}
